<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

class PimModelCustomer extends JModel {
	
	function __construct() {
		parent::__construct();
	}
	
	function count($where) {
		$query = "SELECT count(id) FROM #__customer $where";
		$this->_db->setQuery($query);
		return $this->_db->loadResult();
	}
	
	function getData(&$total, &$dbrows , $order_by = 'id', $order_dir = 'DESC', $offset = 0, $limit = 30, $where = array()) {
		if(sizeof($where) > 0)
			$where = 'WHERE ' . implode(' AND ', $where);
		else
			$where = '';

		if($limit !== 0)
			$limit = "LIMIT $offset, $limit";
		else
			$limit = '';
			
		$query = "SELECT * FROM #__customer $where ORDER BY $order_by $order_dir $limit";

		$this->_db->setQuery($query);
		$dbrows	= $this->_db->loadObjectList();

		$total = $this->count($where);
	}
	
	function getListSelectBox() {
		$query = "SELECT * FROM #__customer ORDER BY id DESC";
		$this->_db->setQuery($query);
		return $this->_db->loadObjectList();
	}
	
	function getListCustomerCode() {
		$query = "SELECT code FROM #__customer";
		$this->_db->setQuery($query);
		return ManagerHelper::getListCode($this->_db->loadObjectList());
	}
	
	function getListType() {
		$arr_status = array(
			ManagerHelper::CUSTOMER_TYPE_AGENCY=>ManagerHelper::CUSTOMER_TYPE_AGENCY_TEXT,
			ManagerHelper::CUSTOMER_TYPE_PERSONAL=>ManagerHelper::CUSTOMER_TYPE_PERSONAL_TEXT
		);
		return $arr_status;		
	}
	
	function ExcelReport($objPHPExcel) {
		$query = $this->createQueryExport(); 

		$this->_db->setQuery($query);
		$rows = $this->_db->loadAssocList();
		
		$row_start = intval(ManagerHelper::ROW_START_EXPORT_EXCEL);
		
		//set title cell
        $objPHPExcel->getActiveSheet()->SetCellValue('A'.$row_start, 'Code');
        $objPHPExcel->getActiveSheet()->SetCellValue('B'.$row_start, 'Name');
        $objPHPExcel->getActiveSheet()->SetCellValue('C'.$row_start, 'Phone');
        $objPHPExcel->getActiveSheet()->SetCellValue('D'.$row_start, 'Address');
        $objPHPExcel->getActiveSheet()->SetCellValue('E'.$row_start, 'Email');
        $objPHPExcel->getActiveSheet()->SetCellValue('F'.$row_start, 'Representative');
        $objPHPExcel->getActiveSheet()->SetCellValue('G'.$row_start, 'Type');

        if (is_array($rows) && count($rows) > 0) {
            $row_data = $row_start + 1;
            foreach ($rows as $data) {
                $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row_data, $data['code']);
                $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row_data, $data['name']);
                $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row_data, $data['phone']);
                $objPHPExcel->getActiveSheet()->SetCellValue('D' . $row_data, $data['address']);
                $objPHPExcel->getActiveSheet()->SetCellValue('E' . $row_data, $data['email']);
                $objPHPExcel->getActiveSheet()->SetCellValue('F' . $row_data, $data['representative']);
                $objPHPExcel->getActiveSheet()->SetCellValue('G' . $row_data, ManagerHelper::getTextTypeCustomer($data['type']));
                
                $row_data++;
            }
        }
        
        return $objPHPExcel;
	}

	function PDFReport() {
		$query = $this->createQueryExport();

		$this->_db->setQuery($query);
		$rows = $this->_db->loadAssocList();

		$data  = '<table border="1" cellspacing="0" cellpadding="0">';
		$data .= '<tr style="background-color:#FFFF00; color:#0000FF; font-weight:bold; text-align:center">' ;		
		$data .= '<th>Code</th>';
		$data .= '<th>Name</th>';
		$data .= '<th>Phone</th>';
		$data .= '<th>Address</th>';
		$data .= '<th>Email</th>';
		$data .= '<th>Representative</th>';
		$data .= '<th>Type</th>';
		$data .= '</tr>';
		
		for($k=0; $k < count( $rows ); $k++) {
			$row = $rows[$k];
			
			$data .= '<tr style="text-align:center">';
			$data .= '<td>' . $row['code'] . '</td>';
			$data .= '<td>' . $row['name'] . '</td>';
			$data .= '<td>' . $row['phone'] . '</td>';
			$data .= '<td>' . $row['address'] . '</td>';
			$data .= '<td>' . $row['email'] . '</td>';
			$data .= '<td>' . $row['representative'] . '</td>';
			$data .= '<td>' . ManagerHelper::getTextTypeCustomer($row['type']) . '</td>';
			$data .= '</tr>';
		}
		$data .= '</table>';
		
		return $data;		
	}
	
	function createQueryExport() {
		$mainframe = JFactory::getApplication();
		$where = $mainframe->getUserState('filter_customers');
		$order_by = $mainframe->getUserStateFromRequest('customers.filter_order', 'filter_order');
		$order_dir = $mainframe->getUserStateFromRequest('customers.filter_order_Dir', 'filter_order_Dir');

		if (empty($where)) {
			$where = '';
		} else {
			$where = ' WHERE ' . implode(' AND ', $where);
		}
		
		if (empty($order_by)) {
			$order_by = 'id';
		}
		
		if (empty($order_dir)) {
			$order_dir = ManagerHelper::ORDER_DIR;
		}
		
		$query = "SELECT * FROM #__customer $where ORDER BY $order_by $order_dir";
		
		return $query;
	}
}